<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\BaseController;
use App\Models\Order;
use App\Models\Reservation;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class ReservationController extends BaseController
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
//        $page=$request->get('page',1);
//        $limit=$request->get('limit',10);
        $orderOn=$request->input('orderOn');
//        $list=DB::table('reservation')->offset($page)->limit($limit)->get();
//        $list= Reservation::with('user')->when($orderOn,function($query) use($orderOn){
//             $query->where('reservation_on',$orderOn);
//        })->offset($page)->limit($limit)->get();
//        $total=DB::table('reservation')->count();

        $list= Reservation::when($orderOn,function($query) use($orderOn){
            $query->where('reservation_on',$orderOn);
        })->with(['user'])->paginate($request->input('limit',10));

       return $this->success($list);
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
       $model  =new \App\Http\Controllers\Api\OrderController();
        $data=$request->all();

       $count=count($data);
       $res=[];
        foreach ($data as $item){
//            unset($item['cate'],$item['content'],$item['banners']);
            $res['order_no']=$model->createOrderOn();
            $count>1 ? $res['product_item'][]=['id'=>$item['id'],'name'=>$item['name'],'cover_pic'=>$item['cover_pic'],'price'=>$item['price']] :
                $res['product_item']=['id'=>$item['id'],'name'=>$item['name'],'cover_pic'=>$item['cover_pic'],'price'=>$item['price']];
//            if($count>1){
//                  $res['product_item'][]=['id'=>$item['id'],'name'=>$item['name'],'cover_pic'=>$item['cover_pic']];
//
//            }else{
//                $res['product_item']=['id'=>$item['id'],'name'=>$item['name'],'cover_pic'=>$item['cover_pic']];
//
//            }
//            $count>1 ? $res['product_item']=json_encode(array_push($res,['id'=>$item['id'],'name'=>$item['name'],'cover_pic'=>$item['cover_pic']]))
//                : $res['product_item']=json_encode(['id'=>$item['id'],'name'=>$item['name'],'cover_pic'=>$item['cover_pic']]);
            $res['status']=10;
            $res['created_at']=time();
            $res['pay_price']=$item['total'];
            $res['pay_mode']=3;
            $res['order_type']=3;
        }


      $res['product_item']=  $count>1 ? $this->array_unique_fb($res['product_item'],['id','name','cover_pic','price']) : $res['product_item'];

        $res['product_item']=json_encode($res['product_item']);

        if(Order::create($res)) return $this->success();
        return $this->apiError();
    }


    /**
     * @param  [type] $array2D [二维数组]
     * @param  [type] $keys    [全部键名]
     * @return [type]          [去重后的数组]
     */
    function array_unique_fb($array2D=[], $keys=[]){
        $temp = [];
        $temp2 = [];
        foreach ($array2D as $k=>$v){
            $v=join(',',$v);
            $temp[$k]=$v;
        }
        $temp=array_unique($temp);

        foreach ($temp as $k => $v){
            $array=explode(',',$v);
            foreach($keys as $index => $key)
            {
                $temp2[$k][$key] = $array[$index];
            }
        }
        return $temp2;
    }
    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        if(DB::table('reservation')->where('id',$id)->update($request->all())){
            return $this->success();
        }
        $this->error();
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        if(DB::table('reservation')->delete($id)) return $this->success();
        $this->error();
    }
}
